<template>
  <page-main class="page-patient-add">
    <van-cell-group title="请认真填写就诊人信息" size="large">
      <van-cell title="绑卡类型" class="card-type" v-if="type == 'add'">
        <van-button v-for="(item,index) in init_data.bingType_list" :key="index" :type="form.bingType == item.id ? 'info' : 'default'" size="small" round @click="changeBingType(item.id)">{{ item.name }}</van-button>
      </van-cell>
      <van-field v-model="form.cardId" label="就诊卡号" placeholder="请输入卡号" v-if="form.bingType == 2 " />
      <van-field v-model="form.custName" label="姓名" placeholder="请输入真实姓名" :readonly="type == 'edit'"/>
      <van-field value="身份证" label="证件类型" placeholder="请输入真实姓名" readonly/>
      <!-- <fly-picker title="证件类型" placeholder="请选择证件类型" :value.sync="form.cert" :list="init_data.certList" @confirm="getCert" /> -->
      <van-field v-model="form.idNo" label="证件号码" placeholder="请输入证件号码" :readonly="type == 'edit'" />
      <van-field v-model="form.phone" label="手机号码" placeholder="请输入手机号码" />
      <fly-picker title="关系" placeholder="请选择与本人关系" :value.sync="form.relationType" :list="init_data.relationType_list" @confirm="getrelationType" />
      <van-field v-model="form.address" label="就诊人地址" placeholder="请输入地址" rows="2" type="textarea" autosize />
      <van-cell title="设为默认就诊人">
        <van-switch v-model="isDefault" />
      </van-cell>
    </van-cell-group>
    <!-- <div class="tips">
      初次添加就诊人请绑定本院就诊卡号，未持有本院就诊卡的患者需到本院自助机或窗口办理就诊卡后方可进行添加。
    </div> -->
    <div class="p-a-15">
      <van-button type="info" block :loading="btn_loading" @click="submitForm">保存</van-button>
      <div class="p-t-15"></div>
      <van-button type="info" plain block :loading="btn_loading" v-if="type== 'edit'" @click="handleDelete">删除就诊人</van-button>
    </div>
  </page-main>
</template>

<script>

export default {
  data(){
    return {
      type: 'add',
      btn_loading: false,
      methods:'post',
      form:{
        cardId: null,
        patientId: null,
        bingType: 1, // 1：电子卡，2：院内就诊卡
        relationType: null,
        custName: null,
        address: null,
        idNo: null,
        phone: null,
        address: null,
        sex: null,
        age: null,
        isDefault: '0',
      },
      isDefault: false,
      init_data:{
        bingType_list:[
          {id: 1, name: '电子健康卡'},
          {id: 2, name: '院内就诊卡'}
        ],
        relationType_list:[
          {id:1, name: '本人'},
          {id:2, name: '父亲'},
          {id:3, name: '母亲'},
        ],
      }
    }
  },
  created(){
    this.getInitData();
    this.getDetail();
    this.type = this.$route.query.type
    if(this.type == 'edit'){
      this.methods = 'put';
    }else{
      this.methods = 'post';
    }
  },
  methods:{
    async getInitData(){
      this.init_data.relationType_list = await this.$util.getDictList('relation_type');
    },
    getDetail(){
      if(this.$route.query.patientId){
        this.$http('get', '/admin/custApi/'+ this.$route.query.patientId, {}, (data,msg) => {
          this.$util.resetForm(this.form, data);
          this.isDefault = data.isDefault == '1' ? true : false;
        })
      }
    },
    changeBingType(id){
      this.form.bingType = id;
      if(id == 0){
        this.form.cardId = null;
      }
    },
    getrelationType(data){

    },
    getCert(data){

    },
    verifyForm(){
      if(!this.$verify.null(this.form.relationType, '请选择关系')){
        return false;
      }
      if(!this.$verify.null(this.form.custName, '请填写姓名')){
        return false;
      }
      if(!this.$verify.identity(this.form.idNo, '请填写正确的身份证号码')){
        return false;
      }
      let obj = this.$util.analysisIdNo(this.form.idNo);
      this.form.age = obj.age;
      this.form.sex = obj.sex;
      if(this.form.bingType == 2){
        if(!this.$verify.null(this.form.cardId, '请填写就诊卡号')){
          return false;
        }
      }
      if(!this.$verify.mobile(this.form.phone, '请填写手机号')){
        return false;
      }
      // if(!this.$verify.null(this.form.address, '请填写就诊人地址')){
      //   return false;
      // }
      this.form.isDefault = Number(this.isDefault);
      return true;
    },
    submitForm(){
      if(!this.verifyForm()){
        return;
      }
      this.btn_loading = true;
      this.$http(this.methods, '/admin/custApi', this.form, (data,msg) => {
        this.$toast.success('提交成功！');
        this.$router.go(-1);
        this.btn_loading = false;
      },msg => {
        this.btn_loading = false;
      })
    },
    handleDelete(){
      this.$dialog.confirm({
        message: '确认删除该就诊人吗？',
      }).then(() => {
        this.btn_loading = false;
        this.$http('delete', '/admin/custApi/'+this.form.cardId, {}, (data,msg) => {
          let currentPatient = localStorage.getItem('currentPatient');
          if(currentPatient){
            let card = JSON.parse(currentPatient);
            if(card.cardId == this.form.cardId){
              localStorage.removeItem('currentPatient');
            }
          }
          this.$toast.success('删除成功！');
          this.$router.go(-1);
          this.btn_loading = false;
        },msg => {
          this.btn_loading = false;
        })
      })
      .catch(() => {
        // on cancel
      });
    }
  }
}
</script>

<style>

</style>